package com.snpic.appaw.module.insure.service.cxOrder;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.snpic.appaw.module.insure.controller.app.cxOrder.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.cxOrder.CxOrderDO;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;

import com.snpic.appaw.module.insure.dal.mysql.cxOrder.CxOrderMapper;

import static com.snpic.appaw.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.snpic.appaw.module.insure.enums.ErrorCodeConstants.CX_ORDER_DO_NOT_EXISTS;

/**
 * 车险订单 Service 实现类
 *
 * @author jijun
 */
@Service
@Validated
public class CxOrderServiceImpl implements CxOrderService {

    @Resource
    private CxOrderMapper cxOrderMapper;

    @Override
    public Long createCxOrderDO(AppCxOrderDOSaveReqVO createReqVO) {
        // 插入
        CxOrderDO cxOrderDO = BeanUtils.toBean(createReqVO, CxOrderDO.class);
        cxOrderMapper.insert(cxOrderDO);

        // 返回
        return cxOrderDO.getId();
    }

    @Override
    public void updateCxOrderDO(AppCxOrderDOSaveReqVO updateReqVO) {
        // 校验存在
        validateCxOrderDOExists(updateReqVO.getId());
        // 更新
        CxOrderDO updateObj = BeanUtils.toBean(updateReqVO, CxOrderDO.class);
        cxOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteCxOrderDO(Long id) {
        // 校验存在
        validateCxOrderDOExists(id);
        // 删除
        cxOrderMapper.deleteById(id);
    }

    @Override
        public void deleteCxOrderDOListByIds(List<Long> ids) {
        // 删除
        cxOrderMapper.deleteByIds(ids);
        }


    private void validateCxOrderDOExists(Long id) {
        if (cxOrderMapper.selectById(id) == null) {
            throw exception(CX_ORDER_DO_NOT_EXISTS);
        }
    }

    @Override
    public CxOrderDO getCxOrderDO(Long id) {
        return cxOrderMapper.selectById(id);
    }

    @Override
    public PageResult<CxOrderDO> getCxOrderDOPage(AppCxOrderDOPageReqVO pageReqVO) {
        return cxOrderMapper.selectPage(pageReqVO);
    }

}